import http from './http'

export const imageCode=(data)=>{
    return http.get('captcha/imageCode',data);
}

//登录
export const login=(data)=>{
    return http.post('login/admin',data);
}


let promise;
//刷新token
export async function refreshToken() {
    if(promise){
        return promise;
    }
    promise = new Promise(async (resolve)=>{
        console.log("刷新token")
        const userStore = useUserStore();
        const refreshToken = userStore.refreshToken;
        const resp =  await http.get("/refresh_token",{
            headers:{
                Authorization:  `Bearer ${refreshToken}`
            },
            __isRefreshToken:true
        });
        resolve (resp.data.code==200);
    });
    promise.finally(()=>{
        promise = null;
    })
    return promise;
       
}

//判断是否是刷新token
export function isRefreshRequest(config){
    return !!config.__isRefreshToken;
}
//手机号码登陆
export const loginByPhone=(data)=>{
    return http.post('/login/phone',data);
}
//登录
export const register=(data)=>{
    return http.post('login',data);
}

